package com.ebei.platform.service.impl;

import java.util.*;

import com.baomidou.mybatisplus.plugins.Page;
import com.ebei.platform.entity.User;
import com.ebei.platform.mapper.UserMapper;
import com.ebei.platform.vo.UserParams;
import com.ebei.platform.vo.UserPassword;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ebei.platform.service.IUserService;

import com.ebei.platform.common.Constants;

/**
 * todo 测试用户服务接口实现类
 *
 * @author crazypenguin
 * @version 1.0
 * @created 2018/4/22
 */

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
	
	@Override
	public boolean deleteAll() {
		return retBool(baseMapper.deleteAll());
	}

	@Override
	public List<User> selectListBySQL() {
		return baseMapper.selectListBySQL();
	}
	
	@Override
	public User selectByAccount(String account) {
		return baseMapper.selectByAccount(account);
	}
	
	private static final PasswordEncoder encoder = new BCryptPasswordEncoder();
	
	@Override
	public int insertUser(User user, User operator) {
		if (user != null) {
			// 查重
			User existUser = baseMapper.selectByAccount(user.getAccount());
			if (existUser != null) {
				return -2;
			}
			user.setStatus(0);
			user.setId(UUID.randomUUID().toString());
			user.setPassword(encoder.encode(Constants.DEFAULT_PASSWORD));
			if (operator != null) {
				user.setCreateUserId(operator.getId());
				user.setCreateTime(new Date());
			}
			int result = baseMapper.insertUser(user);
			return result;
		}
		return -1;
	}
	
	@Override
	public int updateUser(User user, User operator) {
		User existUser = baseMapper.selectOne(user.getId());
		if (existUser != null) {
			if (user.getName() != null) {
				existUser.setName(user.getName());
			}
			if (user.getAvatar() != null) {
				existUser.setAvatar(user.getAvatar());
			}
			if (user.getStatus() != null) {
				existUser.setStatus(user.getStatus());
			}
			if (operator != null) {
				existUser.setUpdateUserId(operator.getId());
				existUser.setUpdateTime(new Date());
			}
			
			return baseMapper.updateUser(existUser);
		}
		return -1;
	}
	
	@Override
	public int changePwd(UserPassword userPassword, User operator) {
		User existUser = baseMapper.selectOne(userPassword.getUserId());
		if (existUser != null) {
			if (encoder.matches(userPassword.getOldPassword(), existUser.getPassword())) {
				existUser.setPassword(encoder.encode(userPassword.getNewPassword()));
				if (operator != null) {
					existUser.setUpdateUserId(operator.getId());
					existUser.setUpdateTime(new Date());
				}
			} else {
				return -2; // 旧密码错误
			}
			return baseMapper.updateUser(existUser);
		}
		return -1;
	}
	
	@Override
	public Page<User> selectPage(Page<User> page, UserParams params) {
		return page.setRecords(baseMapper.selectPage(page, params));
	}
}